package com.zzyk.microCloud.api.controller;

import cn.hutool.core.util.IdUtil;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.utils.FTPUtils;
import com.zzyk.main.utils.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 文件接口
 *
 * @author wss_PC
 * @DESCRIPTION
 * @create 2020/11/26
 */
@RequestMapping("/api/file")
@RestController
public class FileController {
    @Resource
    private FTPUtils ftp;

    @Value("${ftp.logoDir}")
    private String logoDir;
    @Value("${ftp.photoDir}")
    private String photoDir;

    @PostMapping("/uploadImageMini")
    public Message uploadImageAndMini(@RequestParam("image") MultipartFile image,
                                      @RequestParam("type") String type) throws IOException {
        String filename = IdUtil.fastSimpleUUID() + "." + FileUtils.getExt(image.getOriginalFilename());
        String dir = logoDir;
        if ("photo".equals(type)) {
            dir = photoDir;
        }
        ftp.upload(image.getInputStream(), filename, dir);
        ftp.upload(image.getInputStream(), "min" + filename, dir);
        return Message.success().addData("data", dir + "/" + filename);
    }
}